<?php
class Aclass_file {
	//--windows主机自动转码
	function auto_charset(&$str,$mode=1){
		if($mode==1){
			if(Afun_get_os()!='Linux'){$str = Afun_charset($str,'gbk');}
		}else{
			if(Afun_get_os()!='Linux'){$str = Afun_charset($str);}
		}
	}
	//获取文件信息
	function ascanfileinfo($str){
		$ascandir = $this->ascandir($str);
		if($ascandir['file']){
			foreach($ascandir['file'] as $val){
				$vals=$this->afileinfo($ascandir['path'],$val);
				$fileinfo[] = $vals;
			}
			return $fileinfo;
		}
		return false;
	}
	//获取目录列表
	function ascandir($dirpath,$dirsort='0'){
		$this->auto_charset($dirpath);
		if(is_dir($dirpath)){
			$file_list=scandir($dirpath,$dirsort);
			foreach($file_list as $key => $val){
				if($val == ".." || $val=="."){
				}else{
					if (is_dir($dirpath.$val)){
						$str['dir'][]= Afun_charset($val);
					}
					if (is_file($dirpath.$val)){
						$str['file'][] = Afun_charset($val);
					}
				}
			}
			$str['path'] = Afun_charset($dirpath);
		}else{
			$str=false;
		}
		return $str;
	}
	function ascandiro($dirpath,$dirsort='0',$more=1){
		$this->auto_charset($dirpath);
		if(is_dir($dirpath)){
			$file_list=scandir($dirpath,$dirsort);
			foreach($file_list as $key => $val){
				if($val==".." || $val=="."){
				}else{
					if (is_dir($dirpath.$val)){
						$this->auto_charset($val,2);
						$str->dir[]= $val;
					}elseif (is_file($dirpath.$val)){
						$this->auto_charset($val,2);
						if($more==1){
							$str->file[] = $val;
						}else{
							$this->auto_charset($dirpath,2);
							$str->file[] = $this->afileinfo($dirpath,$val);
							$this->auto_charset($dirpath);
						}
					}
				}
			}
			$this->auto_charset($dirpath,2);
			$str->path = $dirpath;
		}else{
			$str=false;
		}
		return $str;
	}
	//获取文件属性
	function afileinfo($path,$str){
		$this->auto_charset($path);
		$this->auto_charset($str);
		if(!file_exists($path.$str)){return false;}
		$val['names']=$str;//获取文件全名字
		$val_ext=pathinfo($str);
		$val['name']=$val_ext['filename'];//获取文件单名字
		//取得文件后缀类型
		if($val_ext['extension']){$val['ext']=$val_ext['extension'];}else{$val['ext']="未知";}
		$val['atime']=fileatime($path.$str);//上次访问时间
		$val['ctime']=filectime($path.$str);//取得文件的 inode 修改时间
		$val['group']=filegroup($path.$str);//取得文件的组
		$val['inode']=fileinode($path.$str);//获取文件的	inode
		$val['mtime']=filemtime($path.$str);//取得文件修改时间
		$val['mtimedate']=date("Y-m-d H:i:s",$val['mtime']);//取得文件修改时间
		$val['owner']=fileowner($path.$str);//取得文件的所有者
		$val['perms']=fileperms($path.$str);//取得文件的权限
		$val['size']=filesize($path.$str);//取得文件大小
		$val['type']=filetype($path.$str);//取得文件类型
		$val['sizes']=Afun_format_size($val['size']);
		$val['path']=$path.$str;//取得文件路径
		$this->auto_charset($val['names'],2);
		$this->auto_charset($val['name'],2);
		$this->auto_charset($val['path'],2);
		return $val;
	}
	
//返回指定路径下(包含子目录)所有文件的全路径列表	格式:一维数组
	function arr_file_list($path,$limit=""){
		if(!realpath($path)){
			return false;
		}
		$data = $this->ascandir($path);
		return $this->arr_file_list_all($data,$limit);
	}
	function arr_file_list_all($data,$limit=""){
		if($data['dir']){
			$i = 0;
			foreach($data['dir'] as $d){
				if($limit > 0 && $i >= $limit){
					break;
				}
				$path = $data['path'].$d.'/';
				$data2 = $this->ascandir($path);
				if($data2['dir']){
					if(empty($str)){
						$str = $this->arr_file_list_all($data2);
					}else{
						$str = array_merge($str,$this->arr_file_list_all($data2));
					}
				}else{
					if(empty($str)){
						$str = $this->arr_all_path($data2,$i,$limit);
					}else{
						$str = array_merge($str,$this->arr_all_path($data2,$i,$limit));
					}
					$i = $i+count($data2['file']);
				}
			}
			return $str;
		}else{
			return $this->arr_all_path($data,'0',$limit);
		}
	}
	//返回文件列表全路径的 一维数组
	function arr_all_path($data,$i='',$limit=''){
		foreach($data['file'] as $val){
			if($limit > 0 && $i >= $limit){
				break;
			}
			$str[] = $data['path'].$val;
			$i++;
		}
		return $str;
	}
	
	
	
	
	//创建文件
	function amkfile($str){
		$str = Afun_charset($str,'gbk');
		$pathinfo = pathinfo($str);
		if(is_file($str)){
			$msg="文件已经存在!";
		}else{
			rename(tempnam($pathinfo['dirname'],$pathinfo['basename']), $str);
			$msg="创建文件成功!";
		}
		return $msg;
	}
	//删除文件
	function aunlink($str){
		$str = Afun_charset($str,'gbk');//转码
		if(file_exists($str)){
			if(unlink($str))
				$msg = '删除成功';
			else
				$msg = '错误';
		}else{
			$msg = 'no文件夹不存在';
		}
		return $msg;
	}
	//文件重命名
	function arename($file1,$file2){
		$file1 = Afun_charset($file1,'gbk');//转码
		$file2 = Afun_charset($file2,'gbk');//转码
		if(!file_exists($file1))
			$msg="文件不存在!";
		else if(rename($file1,$file2))
			$msg="重命名文件成功!";
		return $msg;
	}
	//删除文件夹
	function armdir($str){
		$str = Afun_charset($str,'gbk');//转码
		if(file_exists($str)){
			if($this->armdirr($str))
				$msg = '删除成功';
			else
				$msg = '错误';
		}else{
			$msg = '文件夹不存在';
		}
		return $msg;
	}
	//删除文件夹下的所有文件
	function armdirr($dir) {
		if($objs = glob($dir."/*")){
			foreach($objs as $obj) {
				is_dir($obj)? $this->armdirr($obj) : unlink($obj);
			}
		}
		rmdir($dir);
		return true;
	}
	//删除该文件夹及其里面的所有文件(不删除本文件夹)
	function armdir_all($dir) {
		if($objs = glob($dir."/*")){
			foreach($objs as $obj) {
				is_dir($obj)? $this->armdirr($obj) : unlink($obj);
			}
		}
		rmdir($dir);
		return true;
	}
	//删除该文件夹其里面的所有文件(不删除本文件夹)
	function armdir_child($dir) {
		if($objs = glob($dir."/*")){
			foreach($objs as $obj) {
				is_dir($obj)? $this->armdirr($obj) : unlink($obj);
			}
		}
		return true;
	}

	//--读写文件流
	function read($path,$charset="utf8"){
		$this->auto_charset($path);
		$content = file_get_contents($path);//获取内容
		if($charset == 'gbk'){
			$content = Afun_charset($content);//utf8转gbk后读取
		}
		return $content;
	}
	function write($path,$content,$charset="utf8",$a="w+"){
		$this->auto_charset($path);
		if($charset == 'gbk'){
			$content = Afun_charset($content,'gbk');//utf8转gbk后保存
		}
		$fp = fopen($path, $a );//清空重写方式打开文件流
		fwrite( $fp,$content);//写入文件流
		$msg = '保存成功';
		return $msg;
	}
	function copy($path,$newpath){
		$this->auto_charset($path);
		$this->auto_charset($newpath);
		if(file_exists($path)){copy($path,$newpath);}
	}
	function unlink($path){
		$this->auto_charset($path);
		if(file_exists($path)){
			unlink($path);
			$msg = '删除文件成功！';
		}else{
			$msg = '文件不存在！';
		}
		return $msg;
	}
	function mkfile($path,$note,$charset='utf8'){
		$this->auto_charset($path);
		if(file_exists($path)){
			$msg = '文件已经存在！';
		}else{
			if($charset == 'gbk'){$note = Afun_charset($note,'gbk');}
			file_put_contents($path,$note);
			$msg = '创建文件成功！';
		}
		return $msg;
	}
	function mkdir($path){
		$this->auto_charset($path);
		if(file_exists($path))
			$msg="文件夹已经存在!";
		else if($this->mkdirm($path,2))
			$msg="创建文件夹成功!";
		return $msg;
	}
	function mkdirm($path,$mode=1){
		if($mode==1){$this->auto_charset($path);}
		if(!file_exists($path)){
			$this->mkdirm(dirname($path),2);
			mkdir($path, 0777);
		}
		return true;
	}
	function rmdir($dir,$mode=1){
		if($mode==1){$this->auto_charset($dir);}
		if(file_exists($dir)){
			if($this->rmdirr($dir,2))
				$msg = '删除文件夹成功';
			else
				$msg = '错误';
		}else{
			$msg = '文件夹不存在';
		}
		return $msg;
	}
	function rmdirr($dir,$mode=1) {
		if($mode==1){$this->auto_charset($dir);}
		if($objs = glob($dir."/*")){
			foreach($objs as $obj) {
				is_dir($obj)? $this->rmdirr($obj,2) : unlink($obj);
			}
		}
		rmdir($dir);
		return true;
	}
	//打开文件流
	function aedit_view($path,$charset="utf8"){
		$path = Afun_charset($path,$charset);//转码
		$content = file_get_contents($path);//获取内容
		if($charset == 'gbk'){
			$content = Afun_charset($content);//utf8转gbk后读取
		}
		return $content;
	}
	//保存文件流
	function aedit_save($path,$content,$charset="utf8",$a="w+"){
		$path = Afun_charset($path,'gbk');//转码
		if($charset == 'gbk'){
			$content = Afun_charset($content,'gbk');//utf8转gbk后保存
		}
		$fp = fopen($path, $a );//清空重写方式打开文件流
		fwrite( $fp,$content);//写入文件流
		$msg = '保存成功';
		return $msg;
	}
	

//--保存文件
	function file_save($path,$content,$charset="utf8",$a="w+"){
		$this->amkdirm(dirname($path));
		$path = Afun_charset($path,Acan_os_charset);//转码
		if($charset == 'gbk'){
			$content = Afun_charset($content,'gbk');//utf8转gbk后保存
		}
		$fp = fopen($path, $a );//清空重写方式打开文件流
		if(fwrite( $fp,$content))//写入文件流
			return true;
	}
	//创建文件夹
	function amkdir($str){
		$str = Afun_charset($str,'gbk');
		if(file_exists($str))
			$msg="文件夹已经存在!";
		else if($this->amkdirm($str))
			$msg="创建文件夹成功!";
		return $msg;
	}
	//递归创建文件夹
	function amkdirm($path){
		if(!file_exists($path)){
			$this->amkdirm(dirname($path));
			mkdir($path, 0777);
		}
		return true;
	}
	//返回文件夹大小
	function dirsize($dir){
		$handle=opendir($dir); 
		$size = 0;
		while ( $file=readdir($handle) ) 
		{
			if ( ( $file == "." ) || ( $file == ".." ) ) continue;
			if ( is_dir("$dir/$file") )
				$size += dirsize("$dir/$file");
			else
				$size += filesize("$dir/$file");
		}
		closedir($handle);
		return $size;
	}

	function upfile($upfile,$path,$overwrite=false){
		if (!is_uploaded_file($_FILES["$upfile"]["tmp_name"])){
			echo "文件不存在!";
			exit;
		}
		$file = $_FILES["$upfile"];
		$filenames=$file["name"];
		$filesizes=$file["size"];
		if(!file_exists($path)){$this->mkdirm($path);}
		$filename=$file["tmp_name"];
		$pinfo=pathinfo($file["name"]);
		$realname=$pinfo['filename'];
		$ftype=$pinfo['extension'];
		$savepath = $path.$realname.".".$ftype;
		if (file_exists($savepath) && $overwrite != true){
			echo "同名文件已经存在了".time();
			exit;
		}
		$this->auto_charset($savepath);
		if(!move_uploaded_file ($filename, $savepath)){
			echo "移动文件出错";
			exit;
		}
		$this->auto_charset($savepath,0);
		return $savepath;
	}


	//普通上传
	function aupfile($arr){
		$data		= $arr['data'];
		$filename	= $arr['filename'];
		$webpath	= $arr['webpath'];//url路径
		$maxsize	= $arr['maxsize'];
		$types		= $arr['types'];
		$newname	= $arr['newname'];
		$folder		= $webpath;
		$error		='0';
		$file = $_FILES["$data"];
		$file["name"] = Afun_charset($file["name"],'gbk');
		$file["tmp_name"] = Afun_charset($file["tmp_name"],'gbk');
			$file_info=pathinfo($file["name"]);
			if(!in_array($file_info['extension'], $types)){
				$aupfile['msg']= $data."文件类型不符!".$file["type"];
				$aupfile['result']="0";
				$error = '2';
			}
		if($error != '2'){
			if (!is_uploaded_file($file["tmp_name"])){
				$aupfile['msg']= "图片不存在!";
				$aupfile['result']="0";
				$error = '2';
			}
		}
		if($error != '2'){
			if($maxsize < $file["size"]){
				$aupfile['msg']= "文件太大!".$maxfilesize;
				$aupfile['result']="0";
				$error = '2';
			}
		}
		if($error != '2'){
			if(!file_exists($folder)){
				$this->amkdirm($folder);//文件夹不存在则创建
			}
			$savepath = $folder.$newname.$file["name"];
			if (file_exists($savepath) && $overwrite != true){
				$aupfile['msg']= "同名文件已经存在了";
				$aupfile['result']="0";
				$error = '2';
			}
		}
		if($error != '2'){
			if(move_uploaded_file ($file["tmp_name"], $savepath)){
				$aupfile['msg']= "上传成功";
				$aupfile['result']="1";
				$aupfile['realpath']=Afun_charset($savepath);//返回路劲名(UTF8)
			}else{
				$aupfile['msg'].= $file["tmp_name"]."移动文件出错".$savepath;
				$aupfile['result']="0";
			}
		}
		return $aupfile;
	}

	//删除
	function delete_file($path){
		$path = Afun_charset($path,'gbk');
		if(file_exists($path)){
			unlink($path);
		}
	}

/**
 * @return bool
 * @param string $in
 * @param string $out
 * @param string $param = "1"
 * @desc compressing the file with the zlib-extension
*/
function gzip ($in, $out, $param="1"){
	if (!file_exists ($in) || !is_readable ($in))
		return false;
	if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
		return false;
	$in_file = fopen ($in, "rb");
	if (!$out_file = gzopen ($out, "wb".$param)) {
		return false;
	}
	while (!feof ($in_file)) {
		$buffer = fgets ($in_file, 4096);
		gzwrite ($out_file, $buffer, 4096);
	}
	fclose ($in_file);
	gzclose ($out_file);
	return true;
}

/**
 * @return bool
 * @param string $in
 * @param string $out
 * @desc uncompressing the file with the zlib-extension
*/
	function aunzip($in, $out){
			$in = Afun_charset($in,'gbk');//转码
			$out = Afun_charset($out,'gbk');//转码
		if (!file_exists ($in) || !is_readable ($in))
			return false;
		if ((!file_exists ($out) && !is_writable (dirname ($out)) || (file_exists($out) && !is_writable($out)) ))
			return false;
		$in_file = gzopen ($in, "rb");
		$out_file = fopen ($out, "wb");
		while (!gzeof ($in_file)) {
			$buffer = gzread ($in_file, 4096);
			fwrite ($out_file, $buffer, 4096);
		}
		gzclose ($in_file);
		fclose ($out_file);
		return $msg = '解压成功';
	}
}
?>